<?php

namespace App\Models\Factory\V0;



use App\Helpers\DateUtils;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\V0\User;
use App\Models\Orm\V0\UserCredit;
use App\Models\Orm\V0\UserStatus;


/**
 * 生成恒信订单号
 */
class UserStatusFactory extends AbsModelFactory
{
    /**
     * 更新用户状态
     *
     * @param int $action_code
     * @return bool|string
     */
    public static function addUserStatus($user_id, $data)
    {
        $data['user_id'] = $user_id;
        $result = UserStatus::updateOrCreate([
            'user_id' => $user_id
        ],$data);
        return $result ? true : false;
    }

    //获取用户状态
    public static function getStatus($user_id)
    {
        $status = UserStatus::where('user_id',$user_id)->first()->toArray();
        return $status ? $status : false;
    }

    /**
     * 初始化用户状态表
     * @param $identity
     * @param $labels
     * @param $amount
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public static function initStatus($user_id)
    {
        $data['user_id'] = $user_id;
        $result = UserStatus::firstOrCreate($data);

        return $result ? true : false;

    }

    //获取信用卡的状态
    public  static  function getCreditStatus($user_id)
    {
        $credits = UserCredit::where('user_id',$user_id)->get()->toArray();

        if($credits)
        {

            foreach ($credits as $k =>$credit)
            {
                $data['credit'][$credit['id']] = $credit['bank_token'] ? 2 : 1;;

            }
            return $data;
        }
            return false;
    }
}